package com.xxx.service;

import com.xxx.pojo.*;
import com.xxx.Dao.*;

import com.xxx.util.DbUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/29  14:50
 * @Copyright
 */
public class ClaStuServiceImpl implements clastuSevice {
    @Override
    public int addClaStu(ArrayList<Cla> cla, ArrayList<Student> stu) {
        Connection connection = null;
        try {
            connection = DbUtil.getConnection();
            connection.setAutoCommit(false);//非自动提交
            //原子性操作
            claDoaimpl claDoaimpl = new claDoaimpl();
            ArrayList<Cla> claArrayList = new ArrayList<>();
            claArrayList.add(new Cla("jd13", 11, "2021-07-29 15:30:22"));
            int claId = claDoaimpl.transaction(connection, claArrayList);

            //原子性操作
            StuDomeImpl stuDome = new StuDomeImpl();
            ArrayList<Student> students = new ArrayList<>();
            students.add(new Student(1, "dsf", 36, 178.6, "男", "2021-07-29 18:30:33", claId));
            stuDome.transaction(connection, students);

            connection.commit();
        } catch (SQLException throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return 0;
    }
}
